class Node{
    constructor(element){
        this.element = element;
        this.next = null;
    }
}

class CircularLinkedList{
    constructor(){
        this.head = null;
        this.count = 0;
    }

    //循环迭代链表查找指定元素
    getElementAt(index){
        let current = this.head;
        for(let i = 0;i < index;i++){
            current = current.next;
        }
        return current
    }

    //添加
    push(element){
        let node = new Node(element);
        if(this.head === null){
            this.head = node;
            node.next = this.head;
        }else{
            let last = this.getElementAt(this.count-1);
            last.next = node;
            node.next = this.head;
        }

        this.count++;
    }

    
    //最初一共多少个人 nums
    //数到几淘汰
    countBoy(nums,n){
        
        for(let i = 1;i<=nums;i++){
            this.push(i);
        }
       
        let helper = this.head; //辅助指针
        let current = this.head;//当前指针

        
        while(helper.next != current){
            helper = helper.next; //helper指向最后一个指针
        }
        
        while(this.count > 15){
            for(let i = 0;i < n-1;i++){
                current = current.next;
                helper = helper.next;
            }
            console.log("淘汰的人是",current.element);
            
            helper.next = current.next;
            current = current.next;
            this.count--;
        }
    }
}

let c = new CircularLinkedList();

c.countBoy(30,9);


